import requests
import time
import hashlib
#推荐使用
#session会话，它会自动cookie请求：
sess = requests.session()
# res = sess.request("get",url="http://192.168.1.111:8080/pinter")
# print(type(res))
# print(res.text) #查看响应文本

# print(res.json())#查看json结果

# res.cookies #查看cookies
# res.status_code #查看状态码
# res.url #查看url
# res.raw #查看原始数据
# print(res.url,res.cookies,res.raw) #查看请求头
def studyget():#查询
    #发送get请求接口
    #如果有参数，必须使用字典传参，建议使用params
    params = {"id":1}
    res = sess.request("get",url = "http://192.168.1.111:8080/pinter/com/getSku",params=params)
    print(type(res.text),res.text)
    print(type(res.json()),res.json())

def studypost():#登录
    #发送 表单参数的 post请求
    #post 请求 使用 data 形参
    data = {"userName":"admin","password":"123456"}
    res = sess.request("post",url = "http://192.168.1.111:8080/pinter/com/login",data = data)
    print(res.json())

def studyjson_post():#注册 ，格式同restful中的post,put,delete
    #发送json参数的post请求
    json = {"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"Beijing"}
    res = sess.request("post",url = "http://192.168.1.111:8080/pinter/com/getSku",json = json)
    print(res.json())

def study_key_json_post():
    #发送 表单参数的 post请求
    #post 请求 使用 data 形参
    data = {"param":{"skuId":123,"num":10}}
    res = sess.request("post",url = "http://192.168.1.111:8080/pinter/com/buy",data = data)
    print(res.json())
    
def studyget_json():
    # 发送get请求接口
    # 如果有参数，必须使用字典传参，建议使用params
    params = {"genderType": 0}
    res = sess.request("get",url = "http://192.168.1.111:8080/pinter/com/userList",params=params)
    print(type(res.text),res.text)
    print(type(res.json()),res.json())
    
def restful_get():#restful格式一
    res = sess.request("get", url="http://192.168.1.111:8080/pinter/com/phone/1")
    print(type(res.text), res.text)
    print(type(res.json()), res.json())

# def restful_get():#restful格式二
#     id = 1  # 要查询的手机ID
#     url = f"http://192.168.1.111:8080/pinter/com/phone/{id}"
#     res = sess.request("get", url=url)  # 不需要params参数
#     print(type(res.text), res.text)
#     print(type(res.json()), res.json())

#restful格式中的post,put,delete:
def restful_post():
    json = {"param":{"brand":"Huawei","color":"yellow","memorySize":"64G","cpuCore":"8核","price":"8848","desc":"全新上市"}}
    res = sess.request("post",url = "http://192.168.1.111:8080/pinter/com/phone",json = json)
    print(res.json())
def restful_put():
    json = {"brand":"Huawei","color":"yellow","memorySize":"64G","cpuCore":"8核","price":"8848","desc":"全新上市"}
    res = sess.request("put",url = "http://192.168.1.111:8080/pinter/com/phone",json = json)
    print(res.json())
def restful_delete():
    json ={"brand":"Huawei","color":"yellow","memorySize":"64G","cpuCore":"8核","price":"8848","desc":"全新上市"}
    res = sess.request("delete",url = "http://192.168.1.111:8080/pinter/com/phone",json = json)
    print(res.json())
    
def sign_md5():
    time.time()
    phonenum='18091839960'
    optcode='testfan'
    mytime=str(int(time.time()*1000))
    str1=phonenum+optcode+mytime#加密的规范
    
    m=hashlib.md5(str1.encode())
    sign=m.hexdigest()
    print(sign)
    json={"phoneNum":phonenum,"optCode":"testfan","timestamp":mytime,"sign":sign}
    res=sess.request('post',url= "http://192.168.1.84:8080/pinter/com/userInfo",json = json)
    print(res.json())
    
# def cookie_bank():
#     data = {"userName": "admin", "password": "123456"}
#     res = sess.request("post", url="http://192.168.1.111:8080/pinter/bank/api/login", data=data)
#     print(res.json())
    params={"userName": "admin"}
    res = sess.request("get", url="http://192.168.1.111:8080/pinter/bank/api/query", params=params)
    print(res.json())
    
def token_bank():
    data = {"userName": "admin", "password": "123456"}
    res = sess.request("post", url="http://192.168.1.111:8080/pinter/bank/api/login2", data=data)
    print(res.json()['data'])
    params = {"userName": "admin"}
    headers={'testfan-token':res.json()['data']}
    res = sess.request("get", url="http://192.168.1.111:8080/pinter/bank/api/query2", params=params,headers=headers)
    print(res.json())
    
def file_upload():
    path='day01.py'
    with open(path,mode='rb') as f:
        files={'file':(path,f)}
        res=sess.request("post", url="http://192.168.1.111:8080/pinter/file/api/upload2", files=files)
        print(res.json())
        print(res.text)
        
      
    
if __name__ == "__main__":
    # studyget()
    # studypost()
    # studyjson_post()
    # study_key_json_post()
    # studyget_json()
    # restful_get()
    # restful_post()
    # restful_put()
    # restful_delete()
    # sign_md5()
    # cookie_bank()
    # token_bank()
    file_upload()